delete.ts 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import { createRoute, OpenAPIHono } from '@hono/zod-openapi';
  2. import { z } from '@hono/zod-openapi';
  3. import { AppDataSource } from '@/server/data-source';
  4. import { BelieverService } from '@/server/modules/believers/believer.service';
  5. import { ErrorSchema } from '@/server/utils/errorHandler';
  6. import { authMiddleware } from '@/server/middleware/auth.middleware';
  7. import { AuthContext } from '@/server/types/context';
  8. const GetParams = z.object({
  9. id: z.string().openapi({
  10. param: { name: 'id', in: 'path' },
  11. example: '1',
  12. description: '信徒ID'
  13. })
  14. });
  15. // 路由定义
  16. const routeDef = createRoute({
  17. method: 'delete',
  18. path: '/{id}',
  19. middleware: [authMiddleware],
  20. request: {
  21. params: GetParams
  22. },
  23. responses: {
  24. 204: {
  25. description: '成功删除信徒记录'
  26. },
  27. 404: {
  28. description: '信徒不存在',
  29. content: {
  30. 'application/json': {
  31. schema: ErrorSchema
  32. }
  33. }
  34. },
  35. 500: {
  36. description: '服务器错误',
  37. content: {
  38. 'application/json': {
  39. schema: ErrorSchema
  40. }
  41. }
  42. }
  43. }
  44. });
  45. // 路由实现
  46. const app = new OpenAPIHono<AuthContext>().openapi(routeDef, async (c) => {
  47. try {
  48. const { id } = c.req.valid('param');
  49. const believerService = new BelieverService(AppDataSource);
  50. const deleted = await believerService.deleteBeliever(parseInt(id));
  51. if (!deleted) {
  52. return c.json({ code: 404, message: '信徒不存在' }, 404);
  53. }
  54. return c.body(null, 204);
  55. } catch (error) {
  56. return c.json({
  57. code: 500,
  58. message: error instanceof Error ? error.message : '删除信徒记录失败'
  59. }, 500);
  60. }
  61. });
  62. export default app;